from core import cmds
from PyQt4.QtGui import QAbstractItemDelegate, QItemDelegate, QWidget, QAction, QToolButton, QHBoxLayout, QPixmap, QSizePolicy
from PyQt4.QtCore import SIGNAL, QSize, Qt


class AttrEditorDelegate(QItemDelegate):
    def createEditor(self, parent_widget, style, index):
        if index.model() is None: return
        item = index.model().itemFromIndex(index)
        if item is None or item.data_obj is None: return
        editor_cls = cmds.editor(item.data_obj)
        if editor_cls is None: return
        editor = editor_cls(item.data_obj)
        editor.widget.setParent(parent_widget)
        return editor.widget


class AttrDelegate(QItemDelegate):
    def __init__(self):
        QItemDelegate.__init__(self)
        self.pixmap = QPixmap()
        #self.connect(self, SIGNAL("closeEditor(QWidget *,QAbstractItemDelegate::EndEditHint = QAbstractItemDelegate.NoHint)"), self.on_close_editor)
        
    #def on_close_editor(self, *args):
        #print 'close editor', args    
        
    def get_display_widget(self, index):
        try:
            item = index.model().itemFromIndex(index)
            return item.editor.display 
        except AttributeError, e:
            return None

    def get_editor_widget(self, index):
        try:
            item = index.model().itemFromIndex(index)
            editor = item.get_editor()
            return editor.widget
        except AttributeError, e:
            print 'get_editor_widget', str(e)
             


    def setEditorData (self, widget, index):
        pass
    #    QItemDelegate.setEditorData(self, widget, index)
        #print 'SET EDITOR DATA', widget, index
        #widget.data_changed()

        
    def setModelData (self, widget, model, index):
        pass
    #    QItemDelegate.setModelData(self, widget, model, index)
        #print 'EDIT', widget, model, index
        #widget.widget_edited()
        #item = model.itemFromIndex(index)
        #item.data_obj.set()
    '''
    def paint(self, painter, style, index):
        #display = self.get_display_widget(index)
        #if display is None:
            #print 'PAINT TEXT', index.model().itemFromIndex(index).text(), display, index.model().itemFromIndex(index).data().typeName()
            #print 'STRING', index.model().itemFromIndex(index).data(Qt.DisplayRole).toString()
            #print 'MODEL', index.model()
        return QItemDelegate.paint(self, painter, style, index)
        return
        if style.rect.width() < display.width():
            display.resize(style.rect.width()+5, style.rect.width()+5)
        else:
            display.adjustSize()
        
        pixmap = QPixmap.grabWidget(display)
        painter.save()
        
        painter.drawPixmap(style.rect.x(), style.rect.y(), pixmap)
        painter.restore()
    '''
    def createEditor(self, parent_widget, style, index):
        item = index.model().itemFromIndex(index)
        if item.isEditable() == False:
            return
        editor = self.get_editor_widget(index)
        if editor is None: 
            return
        #editor.setFocusPolicy(Qt.StrongFocus)
        editor.setAutoFillBackground(True)
        editor.setParent(parent_widget)
        print 'CREATE EDITOR', editor
        try:
            print str(editor.text())
        except: pass
        return editor
    
    #def hasClipping(self): return True
    
    def sizeHint(self, style, index):
        try:
            display = self.get_display_widget(index)
            widget_size = display.size()
            return widget_size
        except AttributeError, e:
            return QItemDelegate.sizeHint(self, style, index)

    def updateEditorGeometry(self, editor, option, index):
        editor.setGeometry(option.rect)

class QListEditorDelegate(QItemDelegate):
    
    def createEditor(self, parent_widget, style, index):
        editor = QListEditorWidget(parent_widget)
        editor.setParent(parent_widget)
        return editor
    
    #def setEditorData(self, widget, index):
    #    pass
    
    #def setModelData(self, widget, model, index):
    #    pass
    
    
class QListEditorWidget(QWidget):
    def __init__(self, item, parent=None):
        QWidget.__init__(self, parent)
        
        self.editor_widget = QWidget(self)
        self.remove_button = QToolButton(self)
        
        self.remove_action = QAction(self)
        self.remove_action.setText("-")
        self.connect(self.remove_action, SIGNAL("triggered()"), lambda: self.emit(SIGNAL("remItem"), self.item) )
        self.remove_button.setDefaultAction(self.remove_action)
        
        self.main_layout = QHBoxLayout()
        self.main_layout.setSpacing(0)
        self.main_layout.setMargin(0)
        self.main_layout.addWidget(self.editor_widget)
        self.main_layout.addWidget(self.remove_button)
        
        self.set_item(item)
        
        self.setLayout(self.main_layout)
        
    def set_item(self, item):
        self.item = item
        editor = cmds.editor(item)
        if editor is None:
            return
        editor_inst = editor(item)
        self.editor_inst = editor_inst
        layout = QHBoxLayout()
        layout.setSpacing(0)
        layout.setMargin(0)
        self.editor_widget.setLayout(layout)
        layout.addWidget(editor_inst.widget)